"""Captcha (Completely Automated Public Turing test to tell Computers and
Humans Apart) module for django

Author: Martin Winkler, June 2007
License: BSD
"""

from django.newforms import Widget, Field
from django.http import Http404

class ReverseCaptchaWidget(Widget):
    """A charfield which has style="display:none" set"""
    def render(self, name, value, attrs=None):
        return u'<input type="text" name="%(name)s" value="%(value)s" \
                id="id_%(name)s" sXtyle="display:none" />' % {'name':name,
                'value': value }

class ReverseCaptchaField(Field):
    """the clean method will raise a Http404 error if the entered value is different
    from the initial value."""
    def __init__(self, label='', *args, **kwargs):
        super(ReverseCaptchaField, self).__init__(widget=ReverseCaptchaWidget(), label='', *args, **kwargs)

    def clean(self, value):
        if value != self.initial:
            raise Http404
        return True

